import {createStore} from 'vuex';
import createPersistedState from 'vuex-persistedstate'; // Vuex 数据持久化
import router from '@/router';
import manageApi from '@/api/manage';

interface State {
  sidebarShow: boolean;
  href: string; // 请求地址
  hasLogin: boolean; // 是否登陆
  // eslint-disable-next-line no-undef
  userInfo: GlobalUser | undefined;
  unprocessNum: Number;
  dialogClassNameCount: any;
  dialogzIndex: any;
  isOnline: Boolean;
  messageData: any;
  websocketInt: any;
  messageCount: number;
  blockType: number;
}
export default createStore<State>({
  plugins: [createPersistedState()],
  state: {
    sidebarShow: false, // 侧边栏显示状态
    href: '',
    hasLogin: false,
    userInfo: undefined,
    unprocessNum: 0, // 侧边栏未处理数
    dialogClassNameCount: 0, // 用于生成dialog移动的类名
    dialogzIndex: 1800, // 弹窗的层级问题
    isOnline: false, // 是否在线飞行数据
    messageData: {}, // 消息通知
    websocketInt: '', // websocket实例
    messageCount: 0,
    blockType: 0,
  },
  mutations: {
    setHasLogin(state, login) {
      state.hasLogin = login;
    },
    setUnprocessNum(state, data) {
      state.unprocessNum = data;
    },
    changeDialogClassNameCount(state, data) {
      state.dialogClassNameCount++;
    },
    changeDialogzIndex(state, data) {
      state.dialogzIndex += 2;
    },
    setFlightState(state, data) {
      state.isOnline = data;
    },
    setMessageData(state, data) {
      state.messageData = data;
      if (data.notice) {
        switch (data.notice) {
          case 'system':
            state.messageCount++;
            break;
          default:
            break;
        }
      }
    },
    setWebsocketInt(state, data) {
      state.websocketInt = data;
    },
    setMessageCount(state, data) {
      state.messageCount = data;
    },
    setFlightBlockType(state, data) {
      state.blockType = data;
    },
  },
  actions: {
    logOut(context) {
      context.commit('setHasLogin', false);
      localStorage.clear();
      router.push('/login');
    },
    // 获取后台管理侧边栏未处理数
    getUnprocessNum(context) {
      return new Promise((resolve, reject) => {
        manageApi
          .deviceUnprocess()
          .then((res: any) => {
            context.commit('setUnprocessNum', res.data);
            resolve(res);
          })
          .catch((error: any) => {
            reject(error);
          });
      });
    },
  },
  modules: {},
});
